package com.bw.ppjokeplay.mvvm.viewmodel.home

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.bw.ppjokeplay.App
import com.bw.ppjokeplay.R
import com.bw.ppjokeplay.adapter.HomeListAdapter
import com.bw.ppjokeplay.commen.entity.BaseEntity
import com.bw.ppjokeplay.commen.field.UIField
import com.bw.ppjokeplay.commen.mvvm.viewmodel.BaseViewModel
import com.bw.ppjokeplay.mvvm.model.entity.HomeFeedEntity
import com.bw.ppjokeplay.mvvm.model.home.HomeModel
import com.bw.ppjokeplay.mvvm.view.LiveHomeActivity
import com.bw.ppjokeplay.mvvm.view.home.Commentsactivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/10 0010 上午 11:14
 *
 */
class HomeViewModel : BaseViewModel<HomeModel>() ,OnItemChildClickListener{

    private var pageCount : Int = 12
    private val feedId = 0;
    private val feedType = "all"
    private var userId = 0
    val adapter : HomeListAdapter = HomeListAdapter()
    var homeFeedEntity : HomeFeedEntity?=null
    var flag = true
    var flag2 = true
    override fun initdata() {
        request()
    }

    fun request(){
        m.requestHomeList(feedId, feedType, pageCount, userId)!!
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this)
        adapter.setOnItemChildClickListener(this)
    }

    override fun result(t: BaseEntity) {
        if(t is HomeFeedEntity){
            homeFeedEntity = t as HomeFeedEntity
            adapter.setNewInstance(homeFeedEntity!!.data!!.data)
        }
    }

    fun zb(){
        val bundle = Bundle()
        startPage(LiveHomeActivity::class.java,bundle)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

        val id = view.id
        val imageView = view.findViewById<ImageView>(R.id.imfavorite)
        val imageView2 = view.findViewById<ImageView>(R.id.imdiss)

        when(id){
            R.id.imfavorite->{
                if (flag==false){
                    imageView.setImageResource(R.drawable.icon_cell_like)
                    flag=true
                }else if(flag==true){
                    imageView.setImageResource(R.drawable.icon_cell_liked)
                    flag=false
                }

            }
            R.id.imdiss->{
                if (flag2==false){
                    imageView2.setImageResource(R.drawable.icon_cell_diss)
                    flag2=true
                }else if(flag2==true){
                    imageView2.setImageResource(R.drawable.icon_cell_dissed)
                    flag2=false
                }
            }
            R.id.commentCount->{
                val bundle = Bundle()
                bundle.putLong("id",homeFeedEntity!!.data!!.data!!.get(position).itemId)
                bundle.putString("head",homeFeedEntity!!.data!!.data!!.get(position).author!!.avatar)
                bundle.putString("name",homeFeedEntity!!.data!!.data!!.get(position).author!!.name)
                bundle.putString("text",homeFeedEntity!!.data!!.data!!.get(position).feeds_text)
                bundle.putString("description",homeFeedEntity!!.data!!.data!!.get(position).activityText)
                bundle.putString("url",homeFeedEntity!!.data!!.data!!.get(position).url)
                bundle.putString("img",homeFeedEntity!!.data!!.data!!.get(position).cover)
                startPage(Commentsactivity::class.java,bundle)
            }
        }
    }
}